package org.farm2.skc.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.farm2.base.db.FarmDbFields;
import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.base.exception.FarmAppException;
import org.farm2.base.exception.FarmExceptionUtils;
import org.farm2.base.password.FarmPasswordEncoder;
import org.farm2.skc.dao.SkcKnowVersionDao;
import org.farm2.skc.domain.SkcKnowVersion;
import org.farm2.skc.service.SkcKnowVersionServiceInter;
import org.farm2.tools.bean.FarmBeanUtils;
import org.farm2.tools.db.DataQuery;
import org.farm2.tools.db.DataResult;
import org.farm2.tools.db.commons.DBRule;
import org.farm2.tools.db.commons.DBRuleList;
import org.farm2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**知识版本 
 * @author cbtg自动生成  2025-2-4 12:17:07 
 */
@Service
@Slf4j
public class SkcKnowVersionServiceImpl implements SkcKnowVersionServiceInter {


    @Autowired
    private SkcKnowVersionDao skcKnowVersionDao;

    @Transactional
    @Override
    public SkcKnowVersion insertSkcKnowVersionEntity(SkcKnowVersion skcKnowVersion) {
        FarmDbFields.initInsertBean(skcKnowVersion, FarmUserContextLoader.getCurrentUser());

        skcKnowVersionDao.insert(skcKnowVersion);


        return skcKnowVersion;
    }

    @Transactional
    @Override
    public SkcKnowVersion editSkcKnowVersionEntity(SkcKnowVersion skcKnowVersion) {
        SkcKnowVersion saveSkcKnowVersion = getSkcKnowVersionById(skcKnowVersion.getId());
        FarmExceptionUtils.throwNullEx(saveSkcKnowVersion, I18n.msg("知识版本不存在:?", skcKnowVersion.getId()));
        saveSkcKnowVersion.setId(skcKnowVersion.getId());
        saveSkcKnowVersion.setTitle(skcKnowVersion.getTitle());
        saveSkcKnowVersion.setState(skcKnowVersion.getState());
        saveSkcKnowVersion.setCtime(skcKnowVersion.getCtime());
        saveSkcKnowVersion.setCusername(skcKnowVersion.getCusername());
        saveSkcKnowVersion.setCuserkey(skcKnowVersion.getCuserkey());
        saveSkcKnowVersion.setPcontent(skcKnowVersion.getPcontent());
        saveSkcKnowVersion.setKnowid(skcKnowVersion.getKnowid());
         
        FarmDbFields.initUpdateBean(saveSkcKnowVersion, FarmUserContextLoader.getCurrentUser());
        skcKnowVersionDao.update(saveSkcKnowVersion);
        return saveSkcKnowVersion;
    }

    @Transactional
    @Override
    public SkcKnowVersion getSkcKnowVersionById(String id) {
        return skcKnowVersionDao.findById(id);
    }

    @Override
    public List<SkcKnowVersion> getSkcKnowVersions(DataQuery query) {
        return skcKnowVersionDao.queryData(query.setCount(false)).getData(SkcKnowVersion.class);
    }



    @Transactional
    @Override
    public DataResult searchSkcKnowVersion(DataQuery query) {
        DataResult result = skcKnowVersionDao.queryData(query);
        return result;
    }

    @Override
    public int getSkcKnowVersionNum(DataQuery query) {
        return skcKnowVersionDao.countData(query);
    }


    @Transactional
    @Override
    public void delSkcKnowVersion(String id) {
        /*[tree：树形结构使用]
        if ( skcKnowVersionDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        skcKnowVersionDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  skcKnowVersionDao.countData(query);
    }

    @Override
    public int getNumByKnowId(String knowid) {
        return skcKnowVersionDao.countData(DBRuleList.getInstance().add(new DBRule("KNOWID", knowid, "=")).toList());
    }
    
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {

            SkcKnowVersion node = getSkcKnowVersionById(sourceId);
            if (!"NONE".equals(targetId)) {
                SkcKnowVersion target = getSkcKnowVersionById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            skcKnowVersionDao.update(node);

            List<SkcKnowVersion> list = skcKnowVersionDao.findSubNodes(sourceId);
            for (SkcKnowVersion treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/
    
     /**[tree：树形结构使用]
      * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
        SkcKnowVersion node = skcKnowVersionDao.findById(treeNodeId);
        if (node.getParentid().equals("NONE")) {
            node.setTreecode(node.getId());
        } else {
            node.setTreecode(skcKnowVersionDao.findById(node.getParentid()).getTreecode() + node.getId());
        }
        skcKnowVersionDao.update(node);
    }
     */
    
}
